from fastapi import APIRouter, HTTPException
from src.label.label_services import *
from src.label.label_schemas import *
router = APIRouter()

@router.get("/query", response_model=str)
async def query():
    return "query"

# ✅ 创建：新增或更新一个 Label
@router.post("/save", response_model=str)
def create_or_update_label(label_create: LabelCreate):
    label_id = save_label(label_create)
    if not label_id:
        raise HTTPException(status_code=500, detail="Failed to save label")
    return label_id

@router.post("/save/list", response_model=List[str])
def create_or_update_label(label_create_list: List[LabelCreate]):
    label_id_list = save_labels_batch(label_create_list)
    if not label_create_list:
        raise HTTPException(status_code=500, detail="Failed to save label")
    return label_id_list

# ✅ 查询：获取所有 Labels
@router.get("/list", response_model=List[LabelRDTO])
def read_all_labels():
    return get_all_labels()


# ✅ 查询：根据 ID 获取单个 Label
@router.get("/detail/id", response_model=LabelRDTO)
def read_label(label_id: str):
    label = get_label_by_id(label_id)
    if not label:
        raise HTTPException(status_code=404, detail="Label not found")
    return label


# ✅ 查询：根据 label_code 获取 Label
@router.get("/detail/code", response_model=LabelRDTO)
def read_label_by_code(label_code: str):
    label = get_label_by_code(label_code)
    if not label:
        raise HTTPException(status_code=404, detail="Label not found")
    return label


# ✅ 查询：分页获取 Labels
@router.get("/page", response_model=List[LabelRDTO])
def read_labels_paginated(skip: int = 0, limit: int = 10):
    return get_labels_paginated(skip=skip, limit=limit)


# ✅ 查询：根据 label_id 列表获取多个 Labels
@router.post("/list/ids/", response_model=List[LabelRDTO])
def read_labels_by_ids(label_ids: List[str]):
    return get_labels_by_ids(label_ids)